/* caStore.c */

/*
 * Licensed to the ConArtist Development Team (CADT).
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.
 * The CADT licenses this file to you under the Apache License, 
 * Version 2.0 (the "License"); you may not use this file except in
 * compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <string.h>

#include <apr_strings.h>

#include "conartist.h"
#include "caStore.h"

struct localStore {
    apr_pool_t *pool;           /* pointer to store's memory pool */
    char *local_path;           /* path where data files are stored */
};

apr_status_t openLocalStore(struct caStore *st)
{
    struct localStore *ls = NULL;

    if (!st)
        return APR_EINVAL;

    ls = apr_pcalloc(st->st_pool, sizeof(*ls));
    if (!ls)
        goto clean_exit;

/*
    ls->local_path = apr_pstrdup(st->st_pool, where);
    if (!ls->local_path)
        goto clean_exit;

    st->privateData = ls;
*/
    return APR_SUCCESS;

clean_exit:
    return APR_EGENERAL;
}

apr_status_t localGetPath(char **rStr, const char *path, apr_pool_t *p,
                          void *data)
{
    struct localStore *ls = (struct localStore *)data;

    if (path && path[0] == '/')
        return apr_filepath_merge(rStr, ls->local_path, &path[1], 
                                  APR_FILEPATH_TRUENAME, p);
    else
        return apr_filepath_merge(rStr, ls->local_path, path, 
                                  APR_FILEPATH_TRUENAME, p);

}

struct caStoreModule ConArtistModule = {
    {
        0,                           /* verMajor */
        1,                           /* verMinor */
        "Local File Store",          /* name */
        CAM_STORE,                   /* type */
        NULL,                        /* init */
        NULL,                        /* fini */
    },
    "local",
    &openLocalStore,                 /* openStore */
    NULL,                            /* closeStore */
    &localGetPath,                   /* getPath */
    NULL,                            /* openDir */
    NULL,                            /* readDir */
    NULL,                            /* closeDir */
    NULL,                            /* openFile */
    NULL,                            /* closeFile */
    NULL,                            /* readFile */
    NULL,                            /* writeFile */
};
